/**
 * @description utils api
 * @author chengpeng
 */

const router = require('koa-router')()
const koaForm = require('formidable-upload-koa');
const {
    loginRedirect
} = require('../../middlewares/loginCheckes')
const {
    saveFile
} = require('../../controller/utils')

router.prefix('/api/utils')

router.post('/upload', loginRedirect, koaForm(), async (ctx, next) => {
    const file = ctx.req.files['file']
    if (!file) {
        return
    }
    const {
        size,
        path,
        name,
        type
    } = file
    ctx.body = await saveFile({
        size,
        name,
        type,
        filePath: path
    })
})


module.exports = router